﻿using System.Collections.Generic;
using System.Configuration;
using System.Net;
using System.Net.Mail;
using System.Text;
using EP.Base.BaseService.Manager.Config;
using EP.Base.BaseService.Manager.Interface.Common;

namespace EP.Base.BaseService.Manager.Common
{
    public class Email:IEmail
    {
        private static EmailServerConfigSection _config;
        public Email()
        {
            if (_config==null)
                _config=(EmailServerConfigSection)ConfigurationManager.GetSection(EmailServerConfigSection.SectionName);
        }

        public void Send(string body, string emailSubject, string emailTo)
        {
            Send(body, emailSubject, emailTo.Split(','));
        }

        public void Send(string body, string emailSubject, string[] emailTo)
        {
            Send(body, emailSubject, emailTo, _config.EmailHost, _config.Port, _config.EmailName, _config.EmailFrom, _config.EmailPassword, _config.EnableSsl);
        }

        //private void Send(string body, string emailSubject, IEnumerable<string> emailTo, string emailHost,int port, string emailFrom, string emailPassword)
        //{
        //    if (emailTo == null || !emailTo.Any())
        //        return;
        //    var client = new SmtpClient
        //    {
        //        DeliveryMethod = SmtpDeliveryMethod.Network,
        //        Host = emailHost,
        //        Port = port,
        //        Credentials = new System.Net.NetworkCredential(emailFrom, emailPassword)
        //    };
        //    var message = new MailMessage {From = new MailAddress(emailFrom)};

        //    foreach (string item in emailTo)
        //    {
        //        message.To.Add(new MailAddress(item));
        //    }
        //    message.Subject = emailSubject;
        //    message.Body = body;
        //    message.BodyEncoding = Encoding.UTF8;
        //    message.IsBodyHtml = true;
        //    message.Priority = MailPriority.High;
        //    client.Send(message);
        //}
        public bool Send(string body, string emailSubject, IEnumerable<string> emailTo, string emailHost, int port, string mailName, string emailFrom, string emailPassword, bool enableSsl)
        {
            var message = new MailMessage();
            // 接收人邮箱地址
            foreach (string item in emailTo)
            {
                message.To.Add(new MailAddress(item));
            }
            message.From = new MailAddress(emailFrom, mailName);
            message.BodyEncoding = Encoding.UTF8;
            message.Body = body;
            //GB2312
            message.SubjectEncoding = Encoding.UTF8;
            message.Subject = emailSubject;
            message.IsBodyHtml = true;

            var smtpclient = new SmtpClient(emailHost, port)
            {
                Credentials = new NetworkCredential(emailFrom, emailPassword),
                EnableSsl = enableSsl
            };
            //SSL连接
            smtpclient.Send(message);
            return true;
        }
    }
}
